package com.sc.nft.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.nft.entity.UserCollectionChainLog;
import com.sc.nft.entity.dto.chain.TransferLogDTO;
import com.sc.nft.entity.vo.HoldCollectionDataVO;
import com.sc.nft.entity.vo.TransferVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.Date;
import java.util.List;

/**
 * 藏品链操作轨迹表(sc_user_collection_chain_log)数据Mapper
 *
 * @author kancy
 * @description 由 Mybatisplus Code Generator 创建
 * @since 2022-10-21 00:45:21
 */
@Mapper
public interface UserCollectionChainLogMapper extends BaseMapper<UserCollectionChainLog> {

    int saveBatchUserCollectionChainLogRecord(@Param("list") List<UserCollectionChainLog> list);

    Page<TransferLogDTO> getTransferLog(IPage page, @Param("firstTitle") String firstTitle, @Param("firstTitle") String transactionHash, @Param("address") String address,
                                        @Param("toUserAddress") String toUserAddress, @Param("startTime") Date startTime, @Param("endTime") Date endTime, @Param("collectionType") Integer collectionType);

    Page<TransferVO> getAdminCollectionChainRecord(Page page, @Param("firstTitle") String firstTitle, @Param("fromUserAddress") String fromUserAddress, @Param("toUserAddress") String toUserAddress, @Param("transactionHash") String transactionHash, @Param("collectionType") Integer collectionType, @Param("status") Integer status, @Param("startTime") Date startTime, @Param("endTime") Date endTime);

    /**
     * 获取用户持有藏品的详情统计
     *
     * @param userId
     * @param collection
     * @return
     */
    List<HoldCollectionDataVO> getHoldCollectionData(@Param("userId") Long userId, @Param("collection") String collection);

    /**
     * 用户某一个藏品详情
     *
     * @param userId
     * @param collectionId
     * @return
     */
    List<HoldCollectionDataVO> getSingleHoldCollectionDataByUserIdAndCollectionId(@Param("userId") Long userId, @Param("collectionId") Long collectionId);

    /**
     * 用户某一个藏品分页详情
     * @param page
     * @param userId
     * @param collectionId
     * @return
     */
    Page<HoldCollectionDataVO> getSingleHoldCollectionData(Page page,@Param("userId") Long userId, @Param("collectionId") Long collectionId);

    @Select("SELECT id\n" +
            "FROM (\n" +
            "  SELECT id, ROW_NUMBER() OVER (ORDER BY id) AS row_num\n" +
            "  FROM sc_user_collection_chain_log\n" +
            "  WHERE type = 2 AND status = 1\n" +
            ") AS subquery\n" +
            "WHERE row_num = (SELECT CEIL(COUNT(*)/2) FROM sc_user_collection_chain_log WHERE type = 2 AND status = 1);")
    Long getHalfId();


    @Select("SELECT * from sc_user_collection_chain_log where status=2 and type=2 and token_id=#{tokenId} order by id asc limit 1")
    UserCollectionChainLog getFailByTokenId(@Param("tokenId") Integer tokenId);

}
